﻿Imports System.Object
Imports VSModel
Imports Cls_Common
Imports System.IO
Partial Class FrontEnd_Control_Users_Login
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)


    Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        If chkRemember.Checked Then
            If (Request.Browser.Cookies) Then
                'Check if the cookie with name FRONTENDLOGIN exist on user's machine
                If (Request.Cookies("FRONTENDLOGIN") Is Nothing) Then
                    'Create a cookie with expiry of 30 days                    
                    Response.Cookies("FRONTENDLOGIN").Expires = DateTime.Now.AddDays(30)
                    Response.Cookies("FRONTENDLOGIN").Item("USERNAME") = txtUserName.Text
                Else
                    Response.Cookies("FRONTENDLOGIN").Item("USERNAME") = txtUserName.Text
                End If
            End If
        End If

        'Login here
        'set session

        Using data As New VSEntities
            Try
                Dim strEncryptPass As String = Encrypt(txtPassword.Text)
              
                Dim p As User = (From q In data.Users Where q.UserName = txtUserName.Text.Trim And q.Password = strEncryptPass).SingleOrDefault
                If (Not p Is Nothing) AndAlso (p.IsActivated = True) AndAlso (p.IsAdmin = False) Then
                    Session("UserName") = txtUserName.Text
                    Session("UserID") = p.UserId
                    If p.ApproveVip = True Then
                        Session("UserVip") = p.Vip
                    Else
                        Session("UserVip") = Nothing
                    End If
                    Session("UserCode") = p.Code
                    Dim intUserId As Integer = p.UserId
                    p.LastLogin = Now
                    data.SaveChanges()
                    'Lay menu tu UserMenu
                    'Dim arrMenu() As Integer = (From a In data.UsersMenus Where a.UserId = p.UserId Select a.MenuId).ToArray
                    'Session("MenuId") = arrMenu
                    'Dim arrID() As Integer = (From t In data.UserRoles Where t.UserId = p.UserId Select t.RoleId).ToArray
                    'If arrID.Length > 0 Thef
                    '    Session("RoleID") = arrID
                    '    Dim k = (From q In data.SysFuncRolesStatusPermissions Where arrID.Contains(q.RoleId) Select q.FunctionId).Distinct().ToArray
                    '    Session("Function") = k
                    '    If k.Length <= 0 Then
                    '        Excute_Javascript("Alertbox('Bạn chưa được cấp quyền quản trị.');", Me.Page, True)
                    '        Exit Sub
                    '    End If

                    'Else
                    '    Excute_Javascript("Alertbox('Bạn chưa được cấp quyền quản trị.');", Me.Page, True)
                    '    Exit Sub
                    'End If
                    Dim retURL As String = IIf(Request("retURL") Is Nothing, MainDomain & "my-oto.html", Request("retURL"))
                    Excute_Javascript("init();", Me.Page, True)


                 
                Else
                    Excute_Javascript("alert('Tên đăng nhập hoặc mật khẩu chưa chính xác.');", Me.Page, True)
                 
                End If

            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
            End Try
        End Using
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            ' hidDomain.Value = MainDomain
            'Check if the browser support cookies     
            If Request.Browser.Cookies Then
                If Request.Cookies("FRONTENDLOGIN") IsNot Nothing Then
                    txtUserName.Text = Request.Cookies("FRONTENDLOGIN")("USERNAME").ToString()

                End If
            End If
        End If
    End Sub

 
End Class
